package com.example.onion.hxchat.thread;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.example.onion.hxchat.room.entity.Friend;
import com.example.onion.hxchat.room.entity.User;
import com.example.onion.hxchat.util.HttpUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;
import okhttp3.Request;

/**
 * @author:onion
 * @data: 2020/7/19
 */
public class InitFriendThread implements Runnable {

    private Handler mHandler;


    private String userId;

    private String URL = "http://49.232.60.155:8080/AppServer_war/GetFriendServlet";

    public static final int INIT_FRIEND_FINISH = 3;
    public static final int INIT_FRIEND_FAILED = 4;

    public InitFriendThread(Handler mHandler, String userId) {
        this.mHandler = mHandler;
        this.userId = userId;
    }

    HttpUtils httpUtils = HttpUtils.getInstance();
    @Override
    public void run() {
        FormBody.Builder builder = new FormBody.Builder();
        builder.add("userId", userId);
        FormBody formBody = builder.build();
        httpUtils.asyncPost(URL, formBody, new HttpUtils.HttpCallBack() {
            @Override
            public void onError(Request request, IOException e) {
                Message msg = Message.obtain();
                msg.obj = null;
                msg.what = INIT_FRIEND_FAILED;
                mHandler.sendMessage(msg);
            }

            @SuppressLint("LongLogTag")
            @Override
            public void onSuccess(Request request, String result) {
//                Gson gson = new Gson();
//                //User user = gson.fromJson(result, (Type) User.class);
//                List<Friend> list = new ArrayList<>();
//                JsonArray jsonElements = gson.fromJson(result,JsonArray.class);
//                for (JsonElement j : jsonElements) {
//                    Friend friend = gson.fromJson(j,Friend.class);
//                    list.add(friend);
//                }
                List<Friend> list = parseJSONWithJsonObject(result);
                Message msg = Message.obtain();
                msg.obj = list;
                msg.what = INIT_FRIEND_FINISH;
                mHandler.sendMessage(msg);
                Log.e("PlayUserDetailThread --->user", list.toString());
            }
        });
    }

    @SuppressLint("LongLogTag")
    private List<Friend> parseJSONWithJsonObject(String jsonObject)
    {
        List<Friend> list = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(jsonObject);
            for (int i = 0;i < jsonArray.length();i++)
            {
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                String userOwnerId =jsonObject1.getString("userOwnerId");
                String userId =jsonObject1.getString("userId");
                String userOwnerName =jsonObject1.getString("userOwnerName");
                String userName =jsonObject1.getString("userName");
                Log.e("InitFriendThread -->userId",userId);
                Log.e("InitFriendThread -->userOwnerId",userOwnerId);
                Log.e("InitFriendThread -->userOwnerName",userOwnerName);
                Log.e("InitFriendThread -->userName",userName);
                Friend friend = new Friend(userName,userOwnerName,userId,userOwnerId);
                list.add(friend);
            }
            return list;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }
}
